package com.by.log.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.by.log.entity.LogEntity;
import com.by.log.mapper.LogMapper;
import com.by.log.service.LogService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 日志服务实现类
 * 实现日志相关的业务操作
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, LogEntity> implements LogService {
    
    /**
     * 批量保存日志
     * @param logEntities 日志实体列表
     * @return 保存成功的记录数
     */
    @Override
    public int saveBatchLogs(List<LogEntity> logEntities) {
        return this.saveBatch(logEntities) ? logEntities.size() : 0;
    }
    
    /**
     * 根据追踪ID查询日志
     * @param traceId 追踪ID
     * @return 日志列表
     */
    @Override
    public List<LogEntity> queryByTraceId(String traceId) {
        return this.lambdaQuery()
                .eq(LogEntity::getTraceId, traceId)
                .orderByAsc(LogEntity::getTimestamp)
                .list();
    }
    
    /**
     * 根据时间范围查询日志
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 日志列表
     */
    @Override
    public List<LogEntity> queryByTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
        return this.lambdaQuery()
                .ge(LogEntity::getTimestamp, startTime)
                .le(LogEntity::getTimestamp, endTime)
                .orderByAsc(LogEntity::getTimestamp)
                .list();
    }
}